# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=libgd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.0
pkgrel=3
pkgdesc="GD is an open source code library for the dynamic creation of images by programmers (mingw-w64)"
arch=('any')
url="https://libgd.github.io/"
license=("GPLv2+")
depends=("${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-libimagequant"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-zlib")
replaces=("${MINGW_PACKAGE_PREFIX}-gd")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/libgd/libgd/releases/download/gd-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "libgd-export-pkg-config-build-in-subdirs.patch"
        "mingw-replace-posix_memalign.patch"
        "mingw-getline-link.patch"
        "upstream-622.patch")
sha256sums=('32590e361a1ea6c93915d2448ab0041792c11bae7b18ee812514fe08b2c6a342'
            'ac2786835434b05c72bd826d0cff5e7346b4bd01e78fa22d8b0bea7720c369d2'
            'c3e69c7e59c280e5cc561c984c906b5cfa5ee56aff0b173c0febfdbf90cb048f'
            'b52ec6485d43b511b9b399cda41fc85d5f15f5ebccad1dfc76bb89b451aae512'
            'cfa9ce3683c744d92158f639cf275126b03298a8365b4b46026825045c2db4fb')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #This patch actually does more than simply export dirs into a subdir
  #It also is responsible for creating the pkg_config file and now also
  #the gdlib-config file that some build scripts seem to still require
  #in spite of that mechanism being depreciated.
  patch -Nbp1 -i "${srcdir}/libgd-export-pkg-config-build-in-subdirs.patch"
  patch -Nbp1 -i "${srcdir}/mingw-replace-posix_memalign.patch"
  patch -Nbp1 -i "${srcdir}/mingw-getline-link.patch"

  # https://github.com/libgd/libgd/pull/622
  patch -Nbp1 -i "${srcdir}/upstream-622.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe             \
    -GNinja                                 \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}  \
    -DBUILD_{SHARED,STATIC}_LIBS=ON         \
    -DENABLE_FONTCONFIG=ON                  \
    -DENABLE_PNG=ON                         \
    -DENABLE_JPEG=ON                        \
    -DENABLE_TIFF=ON                        \
    -DENABLE_ICONV=ON                       \
    -DENABLE_XPM=ON                         \
    -DENABLE_FREETYPE=ON                    \
    -DENABLE_WEBP=ON                        \
    -DENABLE_LIQ=ON                         \
    -DENABLE_GD_FORMATS=ON                  \
    -DXPM_XPM_INCLUDE_DIR="${MINGW_PREFIX}/include/X11" \
    -DWEBP_INCLUDE_DIR="${MINGW_PREFIX}/include/webp/" \
    -DLIQ_INCLUDE_DIR="${MINGW_PREFIX}/include/libimagequant" \
    -DFREETYPE_INCLUDE_DIR_freetype2="${MINGW_PREFIX}/include/freetype2" \
    -DFREETYPE_INCLUDE_DIRS="${MINGW_PREFIX}/include/freetype2" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  files=("/lib/pkgconfig/gdlib.pc")
  for f in "${files[@]}"
  do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}${f}
  done
}
